package com.example.springboot_system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot_system.mapper.MedicalExaminationMapper;
import com.example.springboot_system.mapper.OutpatientQueueMapper;
import com.example.springboot_system.mapper.PatientMapper;
import com.example.springboot_system.mapper.RegisterMapper;
import com.example.springboot_system.pojo.*;
import com.example.springboot_system.pojo.vo.MedicalExamination.MedicalExaminationReqVO;
import com.example.springboot_system.pojo.vo.outpatient.AllOutpatientQueueVO;
import com.example.springboot_system.service.MedicalExaminationService;
import com.example.springboot_system.utils.GetId;
import com.example.springboot_system.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class MedicalExaminationServiceImpl implements MedicalExaminationService {

    @Autowired(required = false)
    private RegisterMapper registerMapper;
    @Autowired(required = false)
    private MedicalExaminationMapper medicalExaminationMapper;
    @Autowired(required = false)
    private OutpatientQueueMapper outpatientQueueMapper;
    @Autowired(required = false)
    private PatientMapper patientMapper;

    private GetId getId = new GetId();

    // 提交体检信息
    @Override
    public ResponseResult savePatientExaminationInfo(HttpSession session, MedicalExaminationReqVO medicalExaminationReqVO) {
        System.out.println(medicalExaminationReqVO);
        /* ↓↓↓↓↓↓↓↓↓↓ 空值判断 ↓↓↓↓↓↓↓↓↓↓*/
        User user = (User) session.getAttribute("user");
        String doctorId = user.getId();
        if (doctorId == null || doctorId.equals("")){
            return new ResponseResult(208,"会话已过期");
        }
        String bloodPressure = medicalExaminationReqVO.getBloodPressure();
        if (bloodPressure == null || bloodPressure.equals("")){
            return new ResponseResult(201,"血压为空");
        }
        String bodyTemperature = medicalExaminationReqVO.getBodyTemperature();
        if (bodyTemperature == null || bodyTemperature.equals("")){
            return new ResponseResult(202,"体温为空");
        }
        String pulse = medicalExaminationReqVO.getPulse();
        if (pulse == null || pulse.equals("")){
            return new ResponseResult(203,"脉搏为空");
        }
        String heartRate = medicalExaminationReqVO.getHeartRate();
        if (heartRate == null || heartRate.equals("")){
            return new ResponseResult(204,"心率为空");
        }
        String examinationCost = medicalExaminationReqVO.getExaminationCost();
        if (examinationCost == null || examinationCost.equals("")){
            return new ResponseResult(205,"费用为空");
        }
        /*↑↑↑↑↑↑↑↑↑↑↑ 空值判断 ↑↑↑↑↑↑↑↑↑↑↑*/

        /* ↓↓↓↓↓↓↓↓↓↓ 获取中间值 ↓↓↓↓↓↓↓↓↓↓*/
        String username = user.getUsername();
        /* ↑↑↑↑↑↑↑↑↑↑↑ 获取中间值 ↑↑↑↑↑↑↑↑↑↑↑*/


        /* ↓↓↓↓↓↓↓↓↓↓ 插入数据 ↓↓↓↓↓↓↓↓↓↓ */
        // 1、向examination插入数据
        MedicalExamination medicalExamination = new MedicalExamination();
        medicalExamination.setBloodPressure(medicalExaminationReqVO.getBloodPressure())
                          .setBodyTemperature(medicalExaminationReqVO.getBodyTemperature())
                          .setPulse(medicalExaminationReqVO.getPulse())
                          .setHeartRate(medicalExaminationReqVO.getHeartRate())
                          .setId(UUID.randomUUID().toString())
                          .setCreateDatetime(new Date())
                          .setExaminationCost(Double.parseDouble(medicalExaminationReqVO.getExaminationCost()))
                          .setPrescriptionNum(getId.getPrescriptionNum())
                          .setRegisterId(medicalExaminationReqVO.getRegisterId());
        System.out.println(medicalExamination);
        int row1 = medicalExaminationMapper.insert(medicalExamination);
        if (row1 != 1){
            return new ResponseResult(206,"体检数据插入失败");
        }
        // 2、向outpatientQueue插入数据
//        OutpatientQueue outpatientQueue = new OutpatientQueue();
//        outpatientQueue.setCreateDatetime(new Date())
//                        .setDescription(medicalExaminationReqVO.getPatientName()+"#"+username)
//                        .setId(UUID.randomUUID().toString())
//                        .setOutpatientQueueStatus(0)
//                        .setPatientId(medicalExaminationReqVO.getPatientId())
//                        .setUserId(doctorId)
//                        .setRegisterId(medicalExaminationReqVO.getRegisterId());
//        System.out.println(outpatientQueue);
//        int row2 = outpatientQueueMapper.insert(outpatientQueue);
//        if (row2 != 1){
//            return new ResponseResult(207,"同步数据失败");
//        }

        Register register = new Register();
        register.setId(medicalExaminationReqVO.getRegisterId());
        register.setTreatmentStatus(1);
        registerMapper.updateById(register);

        /* ↑↑↑↑↑↑↑↑↑↑↑ 插入数据 ↑↑↑↑↑↑↑↑↑↑↑*/

        return new ResponseResult(200,"体检完成");
    }


    // 获取就诊队列
    @Override
    public ResponseResult getAllOutpatientQueue(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user.getId() == null || user.getId().equals("")){
            return new ResponseResult(201,"会话已过期，请重新登录");
        }
        String doctorId = user.getId();
        List<Patient> queue = patientMapper.getAllOutpatientQueue(doctorId);
        if (queue == null){
            return new ResponseResult(202,"目前无人挂号");
        }
        System.out.println(queue+"\n");
        return new ResponseResult(200,queue);

//        QueryWrapper<Register> wrapper1 = new QueryWrapper<>();
//        wrapper1.eq("doctor_id",doctorId);
//        wrapper1.eq("treatment_status",0);
//        List<Register> patientIdList = registerMapper.selectList(wrapper1);
//        List<String> list = new ArrayList<>();
//        for (Register r : patientIdList){
//            list.add(r.getPatientId());
//        }
//        QueryWrapper<Patient> wrapper2 = new QueryWrapper<>();
//        List<Patient> patientList = new ArrayList<>();
//        for (String patientId : list){
//            Patient patient = new Patient();
//            wrapper2.eq("id",patientId);
//            patientMapper.selectOne(wrapper2);
//        }
    }

    @Override
    public ResponseResult searchExamin(String cardId, HttpSession session) {
        if(cardId==null || cardId==""){
            return new ResponseResult(201,"请输入卡号");
        }
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("card_id",cardId);
        Patient patient = patientMapper.selectOne(queryWrapper);
        if(patient==null){
            return new ResponseResult(202,"该卡号不存在");
        }
        //查询未收费的挂单号
        QueryWrapper<Register> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("patient_id",patient.getId());
        queryWrapper1.eq("charge_status",0);
        Register register = registerMapper.selectOne(queryWrapper1);
        if(register==null){
            return new ResponseResult(203,"未存在体检记录");
        }
        //在体温表查询
        QueryWrapper<MedicalExamination> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("register_id",register.getId());
        MedicalExamination medicalExamination = medicalExaminationMapper.selectOne(queryWrapper2);
        return new ResponseResult(200,"查询成功",patient,register,medicalExamination);
    }

    @Override
    public ResponseResult MoneyToPayExam(String registerId, HttpSession session) {
        User user1= (User) session.getAttribute("user");
        if(user1==null){
            return new ResponseResult(201,"当前会话已超时，请重新登录");
        }
        Register register = new Register();
        register.setId(registerId);
        register.setChargeStatus(1);
        registerMapper.updateById(register);
        return new ResponseResult(200,"提交成功");
    }


}
